package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.Order;
import com.galaxy.order.domain.OrderDetail;
import com.galaxy.order.domain.OrderReview;
import com.galaxy.order.domain.bo.OrderReviewBo;
import com.galaxy.order.domain.vo.OrderDetailVo;
import com.galaxy.order.domain.vo.OrderReviewVo;
import com.galaxy.order.domain.vo.OrderVo;
import com.galaxy.order.mapper.OrderDetailMapper;
import com.galaxy.order.mapper.OrderMapper;
import com.galaxy.order.mapper.OrderReviewMapper;
import com.galaxy.order.service.OrderReviewService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单评价信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderReviewServiceImpl extends ServiceImpl<OrderReviewMapper, OrderReview> implements OrderReviewService {

    private final OrderReviewMapper orderReviewMapper;
    private final Converter converter;

    private final OrderDetailMapper orderDetailMapper;

    private final OrderMapper orderMapper;

    @DS("slave")
    @Override
    public TableDataInfo<OrderReviewVo> queryPageList(OrderReviewBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderReview> lqw = buildQueryWrapper(bo);
        var page = orderReviewMapper.selectPage(pageQuery.build(), lqw);
        List<OrderReviewVo> records = page.getRecords().stream()
            .map(orderReview -> converter.convert(orderReview, OrderReviewVo.class))
            .collect(Collectors.toList());
        for(OrderReviewVo orderReviewVo : records){
            orderReviewVo.setOrderDetail(
                orderDetailMapper.selectVoList(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderId, orderReviewVo.getOrderId()))
            );
        }
        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderReviewVo> queryList(OrderReviewBo bo) {
        LambdaQueryWrapper<OrderReview> lqw = buildQueryWrapper(bo);
        List<OrderReviewVo> collect = orderReviewMapper.selectList(lqw).stream()
            .map(orderReview -> converter.convert(orderReview, OrderReviewVo.class))
            .collect(Collectors.toList());
        for(OrderReviewVo orderReviewVo : collect){
            orderReviewVo.setOrderDetail(
                orderDetailMapper.selectVoList(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderId, orderReviewVo.getOrderId()))
            );
        }
        return collect;
    }

    @DS("slave")
    @Override
    public OrderReview queryById(Long id) {
        return orderReviewMapper.selectById(id);
    }

    @Override
    public OrderReview getInfoByOrder(Long orderId) {
        return orderReviewMapper.selectOne(new LambdaQueryWrapper<OrderReview>()
            .eq(OrderReview::getOrderId, orderId)
            .last("limit 1"));
    }

    @Override
    public Boolean insertByBo(OrderReviewBo bo) {
        OrderReview add = converter.convert(bo, OrderReview.class);
        orderMapper.update(new LambdaUpdateWrapper<Order>()
            .set(Order::getStatus, 5)
            .eq(Order::getId, bo.getOrderId())
        );
        return orderReviewMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderReviewBo bo) {
        OrderReview update = converter.convert(bo, OrderReview.class);
        return orderReviewMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderReviewMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<OrderReviewVo> queryByProductId(Long productId, PageQuery pageQuery) {
        return TableDataInfo.build(orderReviewMapper.selectByProductId(productId,pageQuery.build()));
    }

    private LambdaQueryWrapper<OrderReview> buildQueryWrapper(OrderReviewBo bo) {
        LambdaQueryWrapper<OrderReview> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderReview::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderReview::getOrderId, bo.getOrderId());
        lqw.eq(bo.getUserId() != null, OrderReview::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, OrderReview::getStatus, bo.getStatus());
        return lqw;
    }
}
